<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>GFX Test: Test the basic utils functions</title>
		<style type="text/css">
			@import "../../../../dojo/resources/dojo.css";
			@import "../../../../dijit/tests/css/dijitTests.css";
		</style>
		<!-- SVGWEB { -->
		<meta name="svg.render.forceflash" content="true"/>
		<script src="svgweb/src/svg.js" data-path="svgweb/src"></script>
		<script src="../../../../dojo/dojo.js" data-dojo-config="isDebug:true,forceGfxRenderer:'svg'" type="text/javascript"></script>
		<!-- } -->
		<script type="text/javascript" src="../../../util/doh/runner.js"></script>
		<script type="text/javascript">
			dojo.require("doh.runner");
			dojo.require("dojox.gfx");
			dojo.require("dojox.gfx.utils");
		</script>
		<script type="text/javascript">
			dojo.addOnLoad(function(){
				var drawing;
				var ta;
				doh.register("GFX: Utils Tests", [
					{
						name: "toJson",
						timeout: 1000,
						setUp: function(){
							if(!drawing){
								var dn = dojo.byId("gfxObject");
								drawing = dojox.gfx.createSurface(dn, 300, 300);
	/* SVGWEB { */
	drawing.whenLoaded(function() {
								drawing.createRect({ 
									 width:  100,
									 height: 100,
									 x: 100,
									 y: 100
								}).setFill("blue").setStroke("black");
	});
	/* } */
							}
							if(!ta){
								ta = dojo.byId("outputArea");
							}
						},
						runTest: function(){
	/* SVGWEB { */
	drawing.whenLoaded(function() {
							var d = new doh.Deferred();
							try{
								var json = dojox.gfx.utils.toJson(drawing);
								doh.assertTrue(json != null, "Checking that non-null was returned.");
								ta.value = json;
								var obj = dojo.fromJson(json);
								doh.assertTrue(1, obj.length, "Checking that the json is an array of one entry.");
								doh.assertTrue(obj[0].shape != null, "Checking that a toplevel shape object is present.");
								doh.assertEqual("rect", obj[0].shape.type, "Checking that the shape type is rect.");
								doh.assertEqual(100, obj[0].shape.width, "Checking that the width is 100.");
								doh.assertEqual(100, obj[0].shape.height, "Checking that the height is 100.");
								d.callback(true);
							}catch(e){
								d.errback(e);
							}
							return d;
	});
	/* } */
						}
					},
					{
						name: "fromJson",
						timeout: 1000,
						setUp: function(){
							if(!drawing){
								var dn = dojo.byId("gfxObject");
								drawing = dojox.gfx.createSurface(dn, 300, 300);
	/* SVGWEB { */
	drawing.whenLoaded(function() {
								drawing.createRect({ 
									 width:  100,
									 height: 100,
									 x: 100,
									 y: 100
								}).setFill("blue").setStroke("black");
	});
	/* } */
							}
							if(!ta){
								ta = dojo.byId("outputArea");
							}
						},
						runTest: function(){
	/* SVGWEB { */
	drawing.whenLoaded(function() {
							var d = new doh.Deferred();
							try{
								var json = dojox.gfx.utils.toJson(drawing);
								doh.assertTrue(json != null, "Checking that non-null was returned.");
								var targetNode = dojo.byId("scratchObject");
								var tempSurface = dojox.gfx.createSurface(targetNode, 300, 300);
								dojox.gfx.utils.fromJson(tempSurface, json);

								var nsJson = dojox.gfx.utils.toJson(tempSurface);
								tempSurface.destroy();

								var obj = dojo.fromJson(nsJson);
								doh.assertTrue(1, obj.length, "Checking that the json is an array of one entry.");
								doh.assertTrue(obj[0].shape != null, "Checking that a toplevel shape object is present.");
								doh.assertEqual("rect", obj[0].shape.type, "Checking that the shape type is rect.");
								doh.assertEqual(100, obj[0].shape.width, "Checking that the width is 100.");
								doh.assertEqual(100, obj[0].shape.height, "Checking that the height is 100.");
								d.callback(true);
							}catch(e){
								d.errback(e);
							}
							return d;
	});
	/* } */
						}
					},
					{
						name: "toSvg",
						timeout: 10000,
						setUp: function(){
							if(!drawing){
								var dn = dojo.byId("gfxObject");
								drawing = dojox.gfx.createSurface(dn, 300, 300);
	/* SVGWEB { */
	drawing.whenLoaded(function() {
								drawing.createRect({ 
									 width:  100,
									 height: 100,
									 x: 100,
									 y: 100
								}).setFill("blue").setStroke("black");
	});
	/* } */
							}
							if(!ta){
								ta = dojo.byId("outputArea");
							}
						},
						runTest: function(){
	/* SVGWEB { */
	drawing.whenLoaded(function() {
							var d = new doh.Deferred();
							var def= dojox.gfx.utils.toSvg(drawing);
							def.addCallback(function(svg){
								try{
									doh.assertTrue(svg != null, "Checking that non-null was returned.");
									ta.value = svg;
									doh.assertTrue(svg.length > 0, "Checking that svg length > 0");
									doh.assertTrue(svg.toLowerCase().indexOf("<svg") === 0, "Checking that the string starts with SVG open tag.");
									d.callback(true);
								}catch(e){
									d.errback(e);
								}
							});
							def.addErrback(function(e){
								d.errback(e);
							});
							return d;
	});
	/* } */
						}
					},
					{
						name: "serialize/deserialize",
						timeout: 1000,
						setUp: function(){
							if(!drawing){
								var dn = dojo.byId("gfxObject");
								drawing = dojox.gfx.createSurface(dn, 300, 300);
	/* SVGWEB { */
	drawing.whenLoaded(function() {
								drawing.createRect({ 
									 width:  100,
									 height: 100,
									 x: 100,
									 y: 100
								}).setFill("blue").setStroke("black");
	});
	/* } */
							}
							if(!ta){
								ta = dojo.byId("outputArea");
							}
						},
						runTest: function(){
	/* SVGWEB { */
	drawing.whenLoaded(function() {
							var d = new doh.Deferred();
							try{
								var sObj = dojox.gfx.utils.serialize(drawing);
								doh.assertTrue(sObj != null, "Checking that non-null was returned.");

								//Lets try to deserialize it!
								var targetNode = dojo.byId("scratchObject");
								var tempSurface = dojox.gfx.createSurface(targetNode, 300, 300);
								dojox.gfx.utils.deserialize(tempSurface, sObj);
								var nsJson = dojox.gfx.utils.toJson(tempSurface);
								tempSurface.destroy();

								var obj = dojo.fromJson(nsJson);
								doh.assertTrue(1, obj.length, "Checking that the json is an array of one entry.");
								doh.assertTrue(obj[0].shape != null, "Checking that a toplevel shape object is present.");
								doh.assertEqual("rect", obj[0].shape.type, "Checking that the shape type is rect.");
								doh.assertEqual(100, obj[0].shape.width, "Checking that the width is 100.");
								doh.assertEqual(100, obj[0].shape.height, "Checking that the height is 100.");
								d.callback(true);
							}catch(e){
								d.errback(e);
							}
							return d;
	});
	/* } */
						}
					}
				]);
				doh.run();
			});
			</script>
	</head>
	<body class="tundra">
		<h1>Test of GFX Utils functions</h1>
		This page is intended for testing of the functions of the utils package, such as toJson, fromJson, and toSvg.
		<div id="gfxObject"></div>
		<div id="scratchObject"></div>
		<div>
			<textarea style="width: 100%; height: 300px" id="outputArea"></textarea>
		</div>
	</body>
</html>
